//
//  ViewController.m
//  20601
//
//  Created by student on 16/3/24.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<NSURLConnectionDataDelegate>
{
    NSMutableData *mdata;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSURL *url=[NSURL URLWithString:@"http://localhost:8080/MJServer/login?username=123&pwd=123&method=get&type=JSON"];
    NSLog(@"协议:%@",url.scheme);
    NSLog(@"主机:%@",url.host);
    NSLog(@"路径:%@",url.relativePath);
    NSLog(@"路径:%@",url.path);
    NSLog(@"端口:%@",url.port);
    NSLog(@"请求:%@",url.query);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)pressed:(id)sender
{
    NSURL *url=[NSURL URLWithString:@"http://localhost:8080/MJServer/login?username=123&pwd=123&method=get&type=JSON"];
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
    NSURLResponse *response;
    NSError *error;
    NSDate *d1=[NSDate date];
    NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSDate *d2=[NSDate date];
    NSTimeInterval t=[d2 timeIntervalSinceDate:d1];
    NSLog(@"花费了%.3f",t);
    NSHTTPURLResponse *httpresponse=response;
    NSLog(@"状态码%i",httpresponse.statusCode);
    NSLog(@"相应头%@",httpresponse.allHeaderFields);
    NSString *str=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@",str);
}

- (IBAction)pressed2:(id)sender
{
    NSURL *url=[NSURL URLWithString:@"http://localhost:8080/MJServer/login?username=123&pwd=123&method=get&type=JSON"];
    
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
    NSDate *d1=[NSDate date];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError){
        NSString *str=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@",str);
    }];
    NSDate *d2=[NSDate date];
    NSTimeInterval t=[d2 timeIntervalSinceDate:d1];
    NSLog(@"花费了:%.3f",t);
}
- (IBAction)pressed3:(id)sender
{
    NSURL *url=[NSURL URLWithString:@"http://localhost:8080/resources/images/minion_02.png"];
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
    NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];
    mdata=[NSMutableData data];
    [connection start];
}
- (IBAction)pressed4:(id)sender
{
    [super didReceiveMemoryWarning];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *h=response;
    NSLog(@"接受到服务器响应%i %@",h.statusCode,h.allHeaderFields);
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
     NSLog(@"接受到服务器返回的数据%i",data.length);
    [mdata appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"接受完成显示到页面");
    UIImage *img=[UIImage imageWithData:mdata];
    UIImageView *imgview=[[UIImageView alloc]initWithImage:img];
    [self.view addSubview:imgview];
    imgview.frame=CGRectMake(100, 100, img.size.width, img.size.height);
}
@end
